package while_lang.ast;

import java.util.Random;

import while_lang.semantics.State;

/** Nodo del AST que representa una negación booleana.
 * 	@author Leonardo Val
 */
public class Negation extends Exp {
	public final Exp operand1;
	
	public Negation(Exp operand1) {
		this.operand1 = operand1;
	}

	@Override public String unparse() { 
		return "(!"+ operand1.unparse() +")";
	}
	
	@Override public Object evaluate(State state) {
		return !(Boolean)this.operand1.evaluate(state);
	}
	
	public static Negation generate(Random random, int min, int max) {
		return new Negation(Exp.generate(random, min-1, max-1));
	}
	
	@Override public String toString() {
		return "Negation("+ operand1 +")";
	}

	@Override public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result	+ ((operand1 == null) ? 0 : operand1.hashCode());
		return result;
	}

	@Override public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Negation other = (Negation) obj;
		if (operand1 == null) {
			if (other.operand1 != null)
				return false;
		} else if (!operand1.equals(other.operand1))
			return false;
		return true;
	}

}
